import { ChangeDetectionStrategy, Component, OnInit, Pipe, PipeTransform } from '@angular/core';
import { moveItemInArray } from '@angular/cdk/drag-drop';
import { uniqueId as _uniqueId, forEach as _forEach, cloneDeep as _cloneDeep, filter as _filter } from 'lodash';
import { LvSizeLSD, OptionItem } from '@iux/live';

interface DemoUser {
  id: string;
  name: string;
  age: number;
  expand: boolean;
  random: number;
  disabled: boolean;
}

@Pipe({ name: 'selectable' })
export class SelectionPipe implements PipeTransform {
  transform(value: DemoUser[], exponent: string = 'disabled') {
    return _filter(value, item => !item[exponent]);
  }
}

@Component({
  selector: 'app-demo25',
  templateUrl: './col-display.component.html',
  styles: [
    `
      h3 {
        margin-top: 16px;
        margin-bottom: 8px;
      }
      h3:first-child {
        margin-top: 0;
      }
      .ui-table-col-select {
        width: 160px;
        margin-bottom: 16px;
      }
      ::ng-deep .ui-table-col-select-panel.lv-select-panel {
        width: 160px;
      }
      .ui-table-col-setting {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 16px;
        height: 30px;
        border-left: 1px solid #f2f2f2;
        background-color: #f5f5f5;
        cursor: pointer;
      }
      ::ng-deep .lv-table-th-container .ui-table-col-setting-active .lv-icon-host:not(.lv-icon-checked) {
        color: #1359bf;
      }
      ::ng-deep
        .lv-table-th-container
        .ui-table-col-setting-active
        .lv-icon-host:not(.lv-icon-checked):not(.lv-table-sorter-asc):not(.lv-table-sorter-presort):not(.lv-table-sorter-desc):not(.active):hover {
        color: #1359bf;
      }
      ::ng-deep .customer-table .lv-table-thead > tr > th:nth-last-child(2) .lv-table-th-deliver {
        display: none;
      }
      .ui-table-setting-title {
        font-size: 16px;
        color: #323233;
        line-height: 24px;
        font-weight: 700;
      }
      .ui-table-setting-content {
        margin: 16px 0;
      }
      .ui-table-setting-content span {
        font-size: 14px;
        color: #1a1a1a;
        line-height: 18px;
        margin-bottom: 4px;
      }
      .ui-table-setting-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 300px;
        height: 32px;
        padding: 0 8px;
        background: #fafafa;
        border-radius: 4px;
        cursor: move;
      }
      .cdk-drop-list-dragging .ui-table-setting-item:not(.cdk-drag-placeholder) {
        transition: transform 200ms cubic-bezier(0, 0, 0.2, 1);
      }
      .cdk-drag-animating {
        transition: transform 200ms cubic-bezier(0, 0, 0.2, 1);
      }
      .lv-drag-placeholder {
        border-color: #2673e5;
        border-radius: 4px;
      }
      .cdk-drag-preview {
        background-color: #f2f2f2;
      }
      .ui-table-setting-content .ui-table-setting-header {
        color: #4d4d4d;
        font-size: 12px;
      }
    `,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ColDisplayComponent implements OnInit {
  // table
  data: DemoUser[];
  columns: OptionItem[] = [];
  oColumns: { [key: string]: any }[] = [];
  selection: DemoUser[];
  currentSize: LvSizeLSD = 'default';

  // select
  showColumns; // 展示的列

  // checkboxGroup
  selected: string[] = []; // 展示的列

  visible = false;

  constructor() {}

  ngOnInit() {
    this.initColumns();
    this.initData();
  }

  initColumns() {
    this.columns = [
      {
        key: 'id',
        label: 'ID',
        disabled: true, // 是否可以隐藏/显示
        hidden: false,
        isLeaf: true,
      },
      {
        key: 'name',
        label: 'Name',
        width: '20%',
        disabled: false,
        hidden: false,
        isLeaf: true,
      },
      {
        key: 'color',
        label: 'Color',
        width: '200px',
        disabled: false,
        hidden: false,
        isLeaf: true,
      },
      {
        key: 'age',
        label: 'Age',
        width: '150',
        disabled: false,
        hidden: false,
        isLeaf: true,
      },
      {
        key: 'random',
        label: 'Random',
        disabled: false,
        hidden: false,
        isLeaf: true,
      },
    ];

    // select实现方式
    this.showColumns = this.columns;

    // checkbox实现方式
    this.oColumns = _cloneDeep(this.columns);
    this.oColumns.forEach(col => {
      !col.hidden && this.selected.push(col.label);
    });
  }

  initData() {
    this.data = Array.from({ length: 5 }).map((item, key) => this.initUser(key, key % 4 === 0));

    // 直接赋值选中项
    this.selection = [this.data[2]];
  }

  initUser(key: number, disabled: boolean) {
    const colors = ['red', 'blue', 'green', 'orange'];
    const temp = {
      id: _uniqueId('uid'),
      name: `user${key}`,
      age: 10 + key,
      expand: false,
      random: parseInt(`${Math.random() * 100}`, 10),
      disabled,
    };
    temp['color'] = colors[temp.random % colors.length];
    return temp;
  }

  onFilterColumns(e) {
    _forEach(this.columns, o => {
      o.hidden = !e.includes(o);
    });
    // 只保存key、hidden和disabled属性
    const columnsKey = this.columns.map(item => ({ key: item.key, hidden: item.hidden, disabled: item.disabled }));
    console.log(e);
  }

  onChangeColumns(e) {
    _forEach(this.oColumns, o => {
      o.hidden = !this.selected.includes(o.label);
      console.log(this.selected.includes(o.label));
    });
  }

  checkAllChange(source) {
    console.log('check all:', source);
  }

  checkOneChange(source) {
    console.log('check one', source);
  }

  selectionChange(source) {
    console.log('selection change', source);
  }

  drop(event) {
    moveItemInArray(this.oColumns, event.previousIndex, event.currentIndex);
  }

  beforeOpen = () => {
    this.visible = true;
    return true;
  };

  afterOpen = () => {
    this.visible = false;
    return true;
  };
}
